unit Core_PluginsEngine;

interface
uses
  Windows, Forms, SysUtils, Core_Types, Core_SettingsEngine, Core_FileOperaions;

var
  AppInterface  : TAppInterface;
  Plugins       : Array of TPluginInterface;
  PluginLoad    : function (AppIntrfc : PAppInterface; PluginInt : PPluginInterface) : Integer;
  RegistredProcs: Array of function:Integer;


procedure LoadPlugins;
function RegisterProc(ProcAddr : Pointer):Integer;

implementation
uses
  Interface_MainWnd, Interface_Console;

function RegisterProc(ProcAddr : Pointer):Integer;
begin
  SetLength(RegistredProcs, Length(RegistredProcs)+1);
  @RegistredProcs[High(RegistredProcs)] := ProcAddr;
  Result := High(RegistredProcs);
end;

procedure LoadPlugins;
var
  sr: TSearchRec;
  hL : THandle;
begin
  @AppInterface.ConsolePrint := @ConsolePrint;
  @AppInterface.SE_SaveSetting := @Core_SaveSetting;
  @AppInterface.CO_GetSizeString := @BytesToSize;
  @AppInterface.UI_AddMenuItem := @UI_AddMenuItem;
  @AppInterface.PE_RegisterProc := @RegisterProc;
  @AppInterface.UI_GetActiveList := @GetActiveList;
  @AppInterface.UI_ListPath := @UI_ListPath;
  @AppInterface.SE_GetStringVal := @Core_Settings_GetStringValue;
  @AppInterface.SE_GetIntVal := @Core_Settings_GetIntegerValue;
  @AppInterface.UI_UpdateLists := @UI_UpdateLists;
  AppInterface._Application := Application;
  
  if FindFirst(ExtractFilePath(ParamStr(0)) + 'Plugins\*.dll', 0, sr) = 0 then
  begin
    repeat
      hL := LoadLibrary(PChar(ExtractFilePath(ParamStr(0)) + 'Plugins\' + sr.Name));
      if hL <> 0 then
      begin
        SetLength(Plugins, Length(Plugins)+1);
        @PluginLoad := GetProcAddress(hL, 'PluginLoad');
        try
          PluginLoad(@AppInterface, @Plugins[High(Plugins)]);
        except
          on E: Exception do WndConsole.PrintLn(E.Message);
        end;
        Plugins[High(Plugins)].hLib := hL;
        WndConsole.PrintLn('Plugin loaded: ' + Plugins[High(Plugins)].Name);
      end;
    until
      FindNext(sr) <> 0;
  end;
end;

end.
